﻿Imports MySql.Data
Imports System.Diagnostics
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class frmitem
    Public MyBarcodeID As String = ""
    Dim Strsql As String = ""
    Dim Mysql1 As New ClsSQLhelper
    Dim On_load As Boolean = True
    Dim On_load_section As Boolean = True
    Dim lRow As Integer
    Dim lCol As Integer
#Region "Function List"
    Private Function GroupStockInfo() As Object
        Throw New NotImplementedException
    End Function

    Sub Load_systemSetting()
        Try
            Dim DS As New DataSet

            Strsql = "select * from systemsetting"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_SystemSetting", "SystemSetting")
            Me.TxtBarcode.MaxLength = DS.Tables(0).Rows(0).Item("BarcodeLengthMax")
            DS.Dispose()
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_systemSetting - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)

        End Try
    End Sub

    Sub Load_Item_Vendor()
        Dim DS As New DataSet
        Try
            If Me.TxtItemNumber.Text <> "" Then
                '    Strsql = "select vendorname,tel,note from vendor left join siteitemvendor on vendor.vendorid  = siteitemvendor.vendorid where active = 1  and siteid = " & My.Settings.CurrentSite & " and siteitemvendor.itemid =  " & Me.TxtItemNumber.Text & "  order by vendorname "
                '    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                '    DS = Mysql1.GetMYSQLDataset(Strsql, "DS_Item_Vendor", "Item_Vendor")
                '    If DS.Tables(0).Rows.Count <> 0 Then Pn_Framework.Set_GridSetting(GridVendor, DS)


                Strsql = "Select vendor.vendorid,Vendorname from item_vendor left join vendor on item_vendor.vendorid = vendor.vendorid "
                Strsql = Strsql & "where  itemnumber = " & Me.TxtItemNumber.Text
                Strsql = Strsql & " order by  Vendorname"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_Item_Vendor", "Item_Vendor")
                '  If DS.Tables(0).Rows.Count = 0 Then Exit Sub
                Pn_Framework.Set_GridSetting(GridVendor, DS)
                GridVendor.Columns(0).Width = 800
                DS.Dispose()
            End If
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_Item_Vendor - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub
    Function Load_ComboboxData_site() As Boolean
        Try
            Dim DS As New DataSet

            Load_ComboboxData_site = False

            Strsql = "select * from site order by site"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_site", "site")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboSite
                '  If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "site"
                .ValueMember = "siteid"
                .SelectionStart = 0
                If My.Settings.CurrentSite <> "" Then
                    .SelectedValue = My.Settings.CurrentSite
                End If
            End With
            Load_ComboboxData_site = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_site - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)

        End Try
    End Function
    Function Load_ComboboxData_Vendor(Optional ByVal StrCondition As String = "") As Boolean
        Try
            Dim DS As New DataSet

            Load_ComboboxData_Vendor = False
            Strsql = "select * from vendor where active = 1 " & StrCondition & "  and siteid = " & My.Settings.CurrentSite & "  order by vendorname "
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_vendor", "vendor")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboVendor
                Me.CboVendor.Text = ""
                ' If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "vendorname"
                .ValueMember = "vendorid"
                .SelectionStart = 0
            End With
            Load_ComboboxData_Vendor = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_band - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Function
    Function Load_ComboboxData_band() As Boolean
        Try
            Dim DS As New DataSet

            Load_ComboboxData_band = False

            Strsql = "select * from band  where active = 1  and siteid = " & Me.CboSite.SelectedValue & " order by band_name"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_band", "band")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboBand
                'If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "band_name"
                .ValueMember = "band_id"
                .SelectionStart = 0
            End With
            Load_ComboboxData_band = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_band - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)

        End Try
    End Function
    Function Load_ComboboxData_section() As Boolean
        Try
            Dim DS As New DataSet
            Load_ComboboxData_section = False
            Strsql = "select * from section where active = 1  and siteid = " & Me.CboSite.SelectedValue & " order by section_name "
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_section", "section")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboSection
                'If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "section_name"
                .ValueMember = "section_id"
            End With
            Load_ComboboxData_section = True
            On_load_section = False
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_section - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Function

    Function Load_ComboboxData_Unit() As Boolean
        Try
            Dim DS As New DataSet

            Load_ComboboxData_Unit = False
            If Me.TxtItemNumber.Text <> "" Then
                Strsql = "select itemunitofmeasure.unitofmeasureid,unitofmeasure,note from itemunitofmeasure left join siteitemunit on itemunitofmeasure.unitofmeasureid = siteitemunit.unitid where itemunitofmeasure.active = 1  and itemunitofmeasure.siteid = " & Me.CboSite.SelectedValue
                Strsql = Strsql & " and siteitemunit.itemid = "
                If Me.TxtItemNumber.Text <> "" Then
                    Strsql = Strsql & Me.TxtItemNumber.Text
                Else
                    Strsql = Strsql & "0"
                End If
                Strsql = Strsql & "  order by itemunitofmeasure.unitofmeasure "

                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_unitofmeasure", "unitofmeasure")
                Pn_Framework.Set_GridSetting(GridUnit, DS)
            End If

            Strsql = "select itemunitofmeasure.unitofmeasureid,unitofmeasure,note from itemunitofmeasure left join siteitemunit on itemunitofmeasure.unitofmeasureid = siteitemunit.unitid where itemunitofmeasure.active = 1  and siteid = " & Me.CboSite.SelectedValue
            If TxtItemNumber.Text <> "" Then
                Strsql = Strsql & " and itemunitofmeasure.unitofmeasureid not in (select unitid  as unitofmeasureid from siteitemunit where itemid = " & Me.TxtItemNumber.Text & ")"
            End If
            Strsql = Strsql & " group by  itemunitofmeasure.unitofmeasure order by itemunitofmeasure.unitofmeasure "
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_unitofmeasure", "unitofmeasure")

            With Me.cbounit
                ' If DS.Tables(0).Rows.Count = 0 Then Exit Function
                Me.cbounit.Text = ""
                .DataSource = DS.Tables(0)
                .DisplayMember = "unitofmeasure"
                .ValueMember = "unitofmeasureid"
            End With

            Load_ComboboxData_Unit = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_Unit - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Function
    Function Load_ComboboxData_category() As Boolean
        Try

            Dim DS As New DataSet
            Load_ComboboxData_category = False

            Strsql = "select * from category where active = 1 and siteid = " & Me.CboSite.SelectedValue
            If On_load_section = False Then
                If Me.CboSection.SelectedValue <> Nothing Then Strsql = Strsql & " and sectionid = " & Me.CboSection.SelectedValue
            End If
            Strsql = Strsql & " order by category_name "

            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_category", "category")

            With Me.CboCategory
                'If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "category_name"
                .ValueMember = "category_id"
            End With
            Load_ComboboxData_category = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_category - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Function
    Function Load_ComboboxData_Manufactur() As Boolean
        Try

            Dim DS As New DataSet
            Load_ComboboxData_Manufactur = False
            Strsql = "select * from manufactur where active = 1  and siteid = " & Me.CboSite.SelectedValue & " order by manufacturname "
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_manufactur", "manufactur")

            With Me.Cbomanufactur
                'If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "manufacturname"
                .ValueMember = "manufacturid"
            End With
            Load_ComboboxData_Manufactur = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_Manufactur - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Function

    Function Prepare_item() As Boolean
        Try
            Prepare_item = False

            'If Load_ComboboxData_Unit() = False Then Exit Function

            Load_ComboboxData_section()
            Load_ComboboxData_category()
            Load_ComboboxData_Manufactur()
            Load_ComboboxData_band()
            Load_ComboboxData_Vendor()
            Load_Item_Vendor()
            Load_ComboboxData_Unit()

            Load_systemSetting()

            Prepare_item = True

        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & "Function Prepare_item = false", EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try

    End Function

    Function Verify_Data() As Boolean
        Dim StrErrMsg As String = ""
        Dim TabPage As TabPage = TabGeneral
        Verify_Data = False

        Try
            StrErrMsg = "กรุณาใส่ชื่อ"
            TabPage = TabGeneral
            If Me.txtitemname.Text = "" Then GoTo Err_Message

            StrErrMsg = "กรุณาใส่ Barcode"
            TabPage = TabGeneral
            If Me.TxtBarcode.Text = "" Then GoTo Err_Message

            StrErrMsg = "กรุณาเลือก ยี่ห้อ"
            TabPage = TabGeneral
            If Me.CboBand.Text = "" Then GoTo Err_Message

            StrErrMsg = "กรุณาเลือก Section"
            TabPage = TabGeneral
            If Me.CboSection.Text = "" Then GoTo Err_Message

            StrErrMsg = "กรุณาเลือก Category"
            TabPage = TabGeneral
            If Me.CboCategory.Text = "" Then GoTo Err_Message

            StrErrMsg = "กรุณาเลือกผู้ผลิต"
            TabPage = TabVendor
            If Me.Cbomanufactur.Text = "" Then GoTo Err_Message

            If Me.TxtItemNumber.Text <> "" Then
                StrErrMsg = "กรุณาเลือกหน่วยนับ"
                TabPage = TabUnit
                If Me.GridUnit.Rows.Count = 0 Then GoTo Err_Message
            End If

            Select Case TxtItemNumber.Text
                Case ""
                    StrErrMsg = "ชื่อ หรือ Barcode นี้มีอยู่แล้ว"
                    Strsql = "Select count(*) from item where itemname = '" & Me.txtitemname.Text & "'  or alternatenumber =  '" & Me.TxtBarcode.Text & "'"
                    If Mysql1.MySQLExecuteScalar(Strsql) > 0 Then GoTo Err_Message
            End Select


            Verify_Data = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & "Function Verify_Data = false", EventLogEntryType.Error, 1)
            MsgBox(ex.Message)

        End Try

        Exit Function
Err_Message:
        MsgBox(StrErrMsg, MsgBoxStyle.Exclamation)
        TabItem.SelectedTab = TabPage
        Exit Function
    End Function


    Sub Save_Data()
        'Dim DS As New DataSet
        Dim StrQuestion As String = ""
        Try
            Select Case TxtItemNumber.Text
                Case ""
                    StrQuestion = "คุณต้องการเพิ่มวัสดุใหม่ใช่หรือไม่"
                Case Else
                    Strsql = "select alternatenumber from item where itemnumber = '" & Me.TxtItemNumber.Text.Trim & "'"
                    Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information, 1)
                    If Mysql1.MySQLExecuteScalar(Strsql) <> Me.TxtBarcode.Text Then
                        If MsgBox("Barcode มีการเปลี่ยนแปลง ท่านต้องยืนยันการบันทึกหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub
                    End If
                    StrQuestion = "คุณต้องการบันทึกข้อมูลใช่หรือไม่"
            End Select


            If MsgBox(StrQuestion, MsgBoxStyle.Question + MsgBoxStyle.YesNo, "บันทึกข้อมูล") = MsgBoxResult.No Then Exit Sub
            Strsql = "select Count(*) from item where itemnumber = '" & Me.TxtItemNumber.Text.Trim & "'"
            Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information, 1)

            Select Case Mysql1.MySQLExecuteScalar(Strsql)
                Case 0
                    frmmain.mSpk.Stop()
                    frmmain.mSpk.Speak(Text:=("ไม่พบข้อมูลเก่า กำลังสร้าง Record ใหม่"))
                    frmmain.mSpk.Play("Write")
                    frmmain.mSpk.Play("RestPose")
                    Strsql = "Insert into item "
                    Strsql = Strsql & " (siteid,itemname,band,description,alternatenumber,Category,itemimage,ref1,ref2,thermo_enable,thermo_hight,thermo_low,active,section,manufactur,minmax_enable,minvalue,maxvalue,standardprice_enable,standdardprice,Limitorder_enable,maxorder,minorder) "
                    Strsql = Strsql & " values ( " & Me.CboSite.SelectedValue & ",'" & Me.txtitemname.Text.Trim & "','" & Me.CboBand.SelectedValue & "','" & Me.TxtDesc.Text & "','" & Me.TxtBarcode.Text.Trim & "'"
                    Strsql = Strsql & ",'" & Me.CboCategory.SelectedValue & "','" & Replace(Me.TxtImgpart.Text.Trim, "\", "\\") & "','" & Me.txtref1.Text & "','" & Me.txtref2.Text & "'"
                    Strsql = Strsql & "," & Me.ChkActiveThermo.Checked & "," & Me.NUD_thermo_hight.Value & "," & Me.NUD_thermo_low.Value & "," & Me.ChkActive.Checked & "," & Me.CboSection.SelectedValue & "," & Me.Cbomanufactur.SelectedValue & "," & Me.ChkActiveInvMinMax.Checked & "," & Me.NUDMin.Value & "," & Me.NUDMax.Value
                    Strsql = Strsql & "," & Me.ChkActiveStandardPrice.Checked & "," & Me.NUStandardprice.Value & "," & Me.ChkActiveOrderMinMax.Checked & "," & Me.NUDOrderMax.Value & "," & Me.NUDOrderMin.Value
                    Strsql = Strsql & " )"
                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 2)
                    Mysql1.MySQLExecute(Strsql)


                    ''Insert SiteItem
                    'For icount As Integer = 0 To GridUnit.Rows.Count - 1
                    '    Strsql = "Insert into siteitem (site,itemnumber,minquantity,maxquantity,reorderquantity,itemunitofmeasure) "
                    '    Strsql = " Values (" & Me.CboSite.SelectedValue & "," & Me.TxtItemNumber.Text & ",0,0,0," & Me.GridUnit.Rows(icount).Cells(0).Value & ")"
                    'Next

                    ' Strsql = "Insert into siteitem () "

                    MsgBox("เพิ่มข้อมูลเรียบร้อยแล้ว กรุณาใส่ หน่วยนับ และ ผู้จำหน่าย", MsgBoxStyle.Information, "บันทึกใหม่")
                    TabItem.SelectedTab = TabUnit
                Case Is > 0
                    frmmain.mSpk.Stop()
                    frmmain.mSpk.Speak(Text:=("พบ 1 record กำลัง update"))
                    frmmain.mSpk.Play("Write")

                    'Use SQL Update data
                    Strsql = "Update item set itemimage = '" & Replace(Me.TxtImgpart.Text.Trim, "\", "\\") & "'"
                    Strsql = Strsql & " ,itemname = '" & Me.txtitemname.Text.Trim & "'"
                    Strsql = Strsql & " ,alternatenumber = '" & Me.TxtBarcode.Text.Trim & "'"
                    Strsql = Strsql & " ,thermo_enable =  " & Me.ChkActiveThermo.Checked
                    Strsql = Strsql & " ,thermo_hight =  " & Me.NUD_thermo_hight.Value
                    Strsql = Strsql & " ,thermo_low =  " & Me.NUD_thermo_low.Value
                    Strsql = Strsql & " ,description = '" & Me.TxtDesc.Text & "'"
                    Strsql = Strsql & " ,ref1 = '" & Me.txtref1.Text & "'"
                    Strsql = Strsql & " ,ref2 = '" & Me.txtref2.Text & "'"
                    Strsql = Strsql & " ,band = " & Me.CboBand.SelectedValue
                    Strsql = Strsql & " ,manufactur=" & Me.Cbomanufactur.SelectedValue
                    Strsql = Strsql & " ,section = " & Me.CboSection.SelectedValue
                    Strsql = Strsql & " ,Category=" & Me.CboCategory.SelectedValue
                    Strsql = Strsql & " ,minmax_enable= " & Me.ChkActiveInvMinMax.Checked
                    Strsql = Strsql & " ,minvalue=  " & Me.NUDMin.Value
                    Strsql = Strsql & " ,maxvalue=  " & Me.NUDMax.Value
                    Strsql = Strsql & " ,active = " & Me.ChkActive.Checked
                    Strsql = Strsql & " ,standardprice_enable = " & Me.ChkActiveStandardPrice.Checked
                    Strsql = Strsql & " ,standdardprice = " & Me.NUStandardprice.Value
                    Strsql = Strsql & " ,Limitorder_enable = " & Me.ChkActiveOrderMinMax.Checked
                    Strsql = Strsql & " ,maxorder = " & Me.NUDOrderMax.Value
                    Strsql = Strsql & " ,minorder = " & Me.NUDOrderMin.Value
                    Strsql = Strsql & " where itemnumber = " & Me.TxtItemNumber.Text.Trim
                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 3)
                    Mysql1.MySQLExecute(Strsql)



                    ''Update SiteItem

                    'Dim icount As Integer
                    'Dim MyDBRows As Integer

                    'For icount = 0 To GridUnit.Rows.Count - 1
                    '    Strsql = "Select count(*) from siteitem where site = " & Me.CboSite.SelectedValue & " and itemnumber = " & Me.TxtItemNumber.Text & " and itemunitofmeasure = " & Me.GridUnit.Rows(icount).Cells(0).Value
                    '    MyDBRows = Mysql1.MySQLExecuteScalar(Strsql)
                    '    Select Case MyDBRows
                    '        Case 0
                    '            Strsql = "Insert into siteitem (site,itemnumber,minquantity,maxquantity,reorderminquantity,reordermaxquantity,itemunitofmeasure) "
                    '            Strsql = Strsql & " Values (" & Me.CboSite.SelectedValue & "," & Me.TxtItemNumber.Text & "," & Me.NUDOrderMin.Value & "," & Me.NUDOrderMax.Value & "," & Me.NUDOrderMin.Value & "," & Me.NUDOrderMax.Value & "," & Me.GridUnit.Rows(MyDBRows).Cells(0).Value & ")"
                    '            Mysql1.MySQLExecute(Strsql)
                    '        Case Is > 0
                    '            Strsql = "Update siteitem set minquantity=" & Me.NUDOrderMin.Value & ",maxquantity=" & Me.NUDOrderMax.Value & ",reorderminquantity=" & Me.NUDOrderMin.Value & ",reordermaxquantity=" & Me.NUDOrderMax.Value & ") "
                    '            Strsql = Strsql & " where site=" & Me.CboSite.SelectedValue & " and itemnumber=" & Me.TxtItemNumber.Text & " and itemunitofmeasure = " & Me.GridUnit.Rows(icount).Cells(0).Value
                    '            Mysql1.MySQLExecute(Strsql)
                    '    End Select

                    'Next

                    MsgBox("บันทึกข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ปรัปปรุงข้อมูล")

                    frmmain.mSpk.Play("RestPose")
                    Me.TxtItemNumber.Focus()
            End Select
            Load_Data()
            Me.TabItem.SelectedTab = TabGeneral
            Me.TxtBarcode.Focus()
        Catch ex As Exception
            MsgBox(ex.Message)
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
        End Try
    End Sub
    Sub New_Data()
        ' Me.TxtBarcode.Text = ""
        Me.txtitemname.Text = ""
        Me.TxtItemNumber.Text = ""
        Me.TxtImgpart.Text = ""
        Me.PictureBox1.Image = WHIS.My.Resources.Resources._2b
        Me.txtref1.Text = ""
        Me.txtref2.Text = ""
        Me.TxtDesc.Text = ""
        Me.NUD_thermo_hight.Value = 0
        Me.NUD_thermo_low.Value = 0
        Me.NUDMax.Value = 0
        Me.NUDMin.Value = 0
        Me.NUDOrderMax.Value = 0
        Me.NUDOrderMin.Value = 0
        Me.NUStandardprice.Value = 0
        Me.ChkActive.Checked = True
        Me.ChkActiveInvMinMax.Checked = False
        Me.ChkActiveOrderMinMax.Checked = False
        Me.ChkActiveStandardPrice.Checked = False
        Me.ChkActiveThermo.Checked = False
        TabItem.SelectedTab = TabGeneral
        Me.CboBand.Text = "ไม่ระบุ"
        Me.CboCategory.Text = "ไม่ระบุ"
        Me.CboSection.Text = "ไม่ระบุ"
        Me.Cbomanufactur.Text = "ไม่ระบุ"
        Me.GridUnit.DataSource = ""
        Me.GridVendor.DataSource = ""
        'If GridUnit.Rows.Count > 0 Then GridUnit.Rows.Clear()
        'If GridVendor.Rows.Count > 0 Then GridVendor.Rows.Clear()
        'If GridStock.Rows.Count > 0 Then GridStock.Rows.Clear()
        Me.TxtBarcode.Focus()
        TxtBarcode.Select(0, TxtBarcode.Text.ToString.Length)
    End Sub
    Sub Load_Data()
        Try
            Enable_Control(False)
            Dim DS As New DataSet
            Dim DT As New DataTable
            Dim Imagepart As String = ""
            ' PictureBox1.Image.Dispose()
            If Me.TxtBarcode.Text.Trim = "" Then Exit Sub
            On_load = True
            New_Data()
            Strsql = "select * from item where alternatenumber = '" & Me.TxtBarcode.Text.Trim & "' and siteid = " & Me.CboSite.SelectedValue
            Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_Item", "item")
            DT = Mysql1.GetMYSQLDataTable(Strsql, "DT_Item")
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            If DS.Tables(0).Rows.Count = 0 Then
                MsgBox("ไม่พบข้อมูล", MsgBoxStyle.Exclamation, "ขออภัย")

                Exit Sub
            End If




            'Found at less 1 record.
            Me.TxtItemNumber.Text = DS.Tables(0).Rows(0).Item("itemnumber").ToString().Trim
            BarCodeCtrl1.BarCode = "  " & Me.TxtBarcode.Text.Trim
            MyBarcodeID = Me.TxtBarcode.Text.Trim

            'GridView

            Load_Item_Vendor()


            'Textbox
            Me.txtref1.Text = DS.Tables(0).Rows(0).Item("ref1").ToString().Trim
            Me.txtref2.Text = DS.Tables(0).Rows(0).Item("ref2").ToString().Trim
            Me.txtitemname.Text = DS.Tables(0).Rows(0).Item("itemname").ToString().Trim


            'Set Standprice
            Me.ChkActiveStandardPrice.Checked = DS.Tables(0).Rows(0).Item("standardprice_enable")
            Me.NUStandardprice.Value = DS.Tables(0).Rows(0).Item("standdardprice").ToString().Trim

            'Set Thermo Low/Hight
            Me.ChkActiveThermo.Checked = DS.Tables(0).Rows(0).Item("thermo_enable")
            Me.NUD_thermo_hight.Value = DS.Tables(0).Rows(0).Item("thermo_hight")
            Me.NUD_thermo_low.Value = DS.Tables(0).Rows(0).Item("thermo_low")

            'Set Inv Min/Max Value 
            Me.ChkActiveInvMinMax.Checked = DS.Tables(0).Rows(0).Item("Limitorder_enable")
            Me.NUDMax.Value = DS.Tables(0).Rows(0).Item("maxvalue")
            Me.NUDMin.Value = DS.Tables(0).Rows(0).Item("minvalue")

            'Set Order Min/Max
            Me.ChkActiveOrderMinMax.Checked = DS.Tables(0).Rows(0).Item("minvalue")
            Me.NUDOrderMax.Value = DS.Tables(0).Rows(0).Item("maxorder")
            Me.NUDOrderMin.Value = DS.Tables(0).Rows(0).Item("minorder")

            'Combobox
            Me.CboBand.SelectedValue = DS.Tables(0).Rows(0).Item("band")
            Me.Cbomanufactur.SelectedValue = DS.Tables(0).Rows(0).Item("manufactur")
            Me.CboSection.SelectedValue = DS.Tables(0).Rows(0).Item("section")
            Me.CboCategory.SelectedValue = DS.Tables(0).Rows(0).Item("category")
            Load_ComboboxData_Unit()
            Load_ComboboxData_Vendor(" and vendorid not in (select vendorid from item_vendor where itemnumber = " & Me.TxtItemNumber.Text & ")")

            'Image
            Imagepart = DS.Tables(0).Rows(0).Item("itemimage").ToString().Trim
            Me.TxtDesc.Text = DS.Tables(0).Rows(0).Item("description").ToString().Trim
            If Imagepart = "" Then
                Me.TxtImgpart.Text = Application.StartupPath.ToString & "\Default.jpg"
            Else
                Me.TxtImgpart.Text = Imagepart
            End If

            Me.ChkActive.Checked = DS.Tables(0).Rows(0).Item("active")

            Enable_Control(True)
            On_load = False

        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub
#End Region
    Private Sub frmitem_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Space Then
            frmsearch.ShowDialog()
            Me.TxtItemNumber.Text = frmsearch.StrItemNo
            frmsearch.Dispose()
        End If
    End Sub
    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
        Me.Dispose()
    End Sub
    Private Sub picZoomin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picZoomin.Click
        FrmFullSize.PictureBox1.Image = Me.PictureBox1.Image
        FrmFullSize.Show()

    End Sub
    Private Sub PicLoadImg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PicLoadImg.Click
        Try
            With Me.OpenFileDialog1
                .Title = "เลือกรูปภาพ"
                .FileName = ""
                .ShowDialog()

                Me.TxtImgpart.Text = .FileName
                Dim bm As New Bitmap(Me.TxtImgpart.Text)
                PictureBox1.Image = bm
            End With
        Catch ex As Exception

        End Try
    End Sub
   

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'Validate Data
        If Verify_Data() = True Then Save_Data()

    End Sub

    Private Sub TxtImgpart_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtImgpart.TextChanged
        Try
            Dim bm As New Bitmap(Me.TxtImgpart.Text.Trim)
            PictureBox1.Image = bm
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

        Catch ex As Exception
            ' MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub BtnDefaultThermo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDefaultThermo.Click
        Me.NUD_thermo_low.Value = 2
        Me.NUD_thermo_hight.Value = 8
    End Sub


    Private Sub BtnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFind.Click
        frmsearch.ShowDialog()
        Me.TxtBarcode.Text = frmsearch.StrItemNo
        Load_Data()
        frmsearch.Dispose()
    End Sub



    Private Sub btnManuf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnManuffind.Click
        frmmanufactur.ShowDialog()
        Load_ComboboxData_Manufactur()
    End Sub

    Private Sub BtnCategoryfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCategoryfind.Click
        frmcategory.ShowDialog()
        Load_ComboboxData_category()
    End Sub

    Private Sub frmitem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Dim DS As New DataSet

        Try

            Me.EventLog1.Source = Application.ProductName




            Load_ComboboxData_site()

            If My.Settings.CurrentuserGroup <> 1 Then Me.CboSite.Enabled = False
            Prepare_item()

            'Me.Dispose()
            If My.Settings.CurrentuserGroup = 3 Then
                Me.TabAdvance.Hide()
                Me.TabInventory.Hide()
                Me.TabVendor.Hide()
                Exit Sub
            Else
                Me.TabAdvance.Show()
                Me.TabInventory.Show()
                Me.TabVendor.Show()
            End If


            New_Data()
            'frmmain.mSpk.Stop()
            'frmmain.mSpk.Play("Suggest")
            'frmmain.mSpk.SoundEffectsOn = False
            'frmmain.mSpk.Speak("สามารถ ค้นหาวัสดุได้โดยการกด spacebar ที่ช่อง  Barcode #")

            On_load = False

        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)

        End Try

    End Sub
    Sub Enable_Control(ByVal EnableControl As Boolean)

        'Advance Setting
        Me.ChkActiveInvMinMax.Enabled = EnableControl
        Me.ChkActiveOrderMinMax.Enabled = EnableControl
        Me.ChkActiveStandardPrice.Enabled = EnableControl
        Me.ChkActiveThermo.Enabled = EnableControl

        'vendor
        Me.CboVendor.Enabled = EnableControl
        Me.BtnAdd.Enabled = EnableControl
        Me.BtnDeleteVendor.Enabled = EnableControl
        Me.BtnManageVendor.Enabled = EnableControl
        Me.GridVendor.Enabled = EnableControl

        'Unit
        Me.cbounit.Enabled = EnableControl
        Me.BtnAddUnit.Enabled = EnableControl
        Me.BtnRemoveUnit.Enabled = EnableControl
        Me.BtnManageUnit.Enabled = EnableControl
        Me.GridUnit.Enabled = EnableControl


    End Sub

    Private Sub TxtBarcode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtBarcode.KeyDown
        Try

            If e.KeyCode = Keys.Space Then
                frmsearch.ShowDialog()
                Me.TxtBarcode.Text = frmsearch.StrItemNo
                frmsearch.Dispose()
            ElseIf e.KeyCode = Keys.Enter Then
                Load_Data()
                TxtBarcode.Select(0, TxtBarcode.Text.ToString.Length)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub TxtBarcode_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TxtBarcode.MouseClick
        TxtBarcode.Select(0, TxtBarcode.Text.ToString.Length)
    End Sub



    Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
        Frmcapture.ShowDialog()
        Me.TxtImgpart.Text = Frmcapture.MyCapturepart
        Frmcapture.Dispose()
    End Sub

    Private Sub BtnSectionFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSectionFind.Click
        frmsection.ShowDialog()
        Load_ComboboxData_section()

    End Sub

    Private Sub BtnDefaultMinMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.NUDMin.Value = 2
        Me.NUDMax.Value = 8
    End Sub

    Private Sub BtnBandFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBandFind.Click
        frmband.ShowDialog()
        Load_ComboboxData_band()
    End Sub

    Private Sub BtnPrint_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
        Try
            'frmPrintBarcode.mybarcode = Me.TxtBarcode.Text
            ' frmPrintBarcode.Show()
        Catch ex As Exception
            MsgBox(ex.Message)
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
        End Try
    End Sub

    Private Sub TxtDesc_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtDesc.TextChanged

    End Sub


    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        If MsgBox("คุณต้องการเพิ่มข้อมูลใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการเพิ่มข้อมูล") = MsgBoxResult.No Then Exit Sub
        Try

            Dim DS As New DataSet
            Strsql = "Insert Into item_vendor (itemnumber,vendorid) values (" & Me.TxtItemNumber.Text & "," & Me.CboVendor.SelectedValue & ")"
            Mysql1.MySQLExecute(Strsql)

            Load_Item_Vendor()

            Load_ComboboxData_Vendor(" and vendorid not in (select vendorid from item_vendor where itemnumber = " & Me.TxtItemNumber.Text & ")")
            MsgBox("บันทึกเรียบร้อย", MsgBoxStyle.Information, "ผลการบันทึก")

        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub BtnPrintBarcode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrintBarcode.Click
        Try
            If Me.TxtBarcode.Text.Trim.Length = 0 Then Exit Sub
            'BarCodeCtrl1.Print()
            Dim MyBarcode As New Cls_Barcode
            If DlgTotalPage.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
            MyBarcode.Print_Barcode(Me.TxtBarcode.Text, DlgTotalPage.NUDTotalPage.Value)
        Catch ex As Exception
            MsgBox(ex.Message)
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
        End Try
    End Sub
    Private Sub CboSection_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboSection.SelectedIndexChanged
        If On_load = False Then Exit Sub
        Call Load_ComboboxData_category()
    End Sub



    Private Sub BtnManageUnit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnManageUnit.Click
        frmUnitofmeasure.ShowDialog()
        Load_ComboboxData_Unit()
    End Sub


    Private Sub CboBand_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboBand.SelectedIndexChanged
        If On_load = True Then Exit Sub
        Strsql = "Select manufactur_id from band where band_id = " & Me.CboBand.SelectedValue
        Me.Cbomanufactur.SelectedValue = Mysql1.MySQLExecuteScalar(Strsql)
    End Sub

    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
        New_Data()
    End Sub

    Private Sub ChkActiveThermo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkActiveThermo.CheckedChanged
        With ChkActiveThermo
            If .Checked = True Then
                Me.Groupthermo.Enabled = True
            Else
                Me.Groupthermo.Enabled = False
            End If
        End With

    End Sub

    Private Sub ChkActiveInvMinMax_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkActiveInvMinMax.CheckedChanged
        With ChkActiveInvMinMax
            If .Checked = True Then
                Me.GroupInv.Enabled = True
            Else
                Me.GroupInv.Enabled = False
            End If
        End With
    End Sub

    Private Sub ChkActiveStandardPrice_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkActiveStandardPrice.CheckedChanged

        With ChkActiveStandardPrice
            If .Checked = True Then
                Me.GroupPrice.Enabled = True
            Else
                Me.GroupPrice.Enabled = False
            End If
        End With
    End Sub

    Private Sub ChkActiveOrderMinMax_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkActiveOrderMinMax.CheckedChanged
        With ChkActiveOrderMinMax
            If .Checked = True Then
                Me.GroupOrder.Enabled = True
            Else
                Me.GroupOrder.Enabled = False
            End If
        End With
    End Sub


    Private Sub BtnRemoveUnit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRemoveUnit.Click
        If MsgBox("คุณต้องการลบข้อมูลใช่หรือไม่?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No Then Exit Sub
        Strsql = "Delete from siteitemunit where itemid= " & Me.TxtItemNumber.Text & " and unitid = " & Me.GridUnit.Rows(lRow).Cells(0).Value
        Mysql1.MySQLExecute(Strsql)
        Load_Data()
        MsgBox("ลบข้อมูลเรียบร้อยแล้ว")
    End Sub

    Private Sub BtnManageVendor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnManageVendor.Click
        FrmVendor.ShowDialog()
        Load_ComboboxData_Vendor()
    End Sub

    Private Sub BtnAddUnit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAddUnit.Click
        Try
            If cbounit.Items.Count = 0 Then
                MsgBox("ไม่มีหน่วยนับให้เลือกเพิ่ม", MsgBoxStyle.Exclamation, "ไม่สามารถทำรายการได้")
                Exit Sub
            End If
            Dim DS As New DataSet
            Strsql = "Select * from siteitemunit where itemid= " & Me.TxtItemNumber.Text & " and unitid = " & Me.cbounit.SelectedValue
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_siteitemunit", "siteitemunit")
            If DS.Tables(0).Rows.Count <> 0 Then
                MsgBox("หน่วยนับ นี้มีอยู่แล้วไม่สามารถบันทึกซ้ำได้อีก", MsgBoxStyle.Critical)
            Else
                Dim MyItemCode As String = ""
                Dim MyUnitCode As String = ""
                Dim i As Integer
                For i = 1 To (5 - Len(Me.TxtItemNumber.Text))
                    MyItemCode = MyItemCode & "0"
                Next
                MyItemCode = MyItemCode & Me.TxtItemNumber.Text

                For i = 1 To (5 - Len(Me.cbounit.SelectedValue.ToString))
                    MyUnitCode = MyUnitCode & "0"
                Next
                MyUnitCode = MyUnitCode & Me.cbounit.SelectedValue.ToString

                Strsql = "Insert into siteitemunit (itemcode,itemid,unitid) values ('" & MyItemCode & MyUnitCode & "'," & Me.TxtItemNumber.Text & "," & Me.cbounit.SelectedValue & ")"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)
                Load_ComboboxData_Unit()
                MsgBox("เพิ่มข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            End If
            DS.Dispose()

            'Refresh Data

        Catch ex As Exception

        End Try
    End Sub

    Private Sub CboSite_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboSite.SelectedIndexChanged
        If On_load = False Then
            On_load = True
            BtnNew_Click(sender, e)
            Prepare_item()
            My.Settings.CurrentSite = Me.CboSite.SelectedValue
            On_load = False
        End If
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        ' If MsgBox("คุณต้องการลบข้อมูลใช่หรือไม่?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No Then Exit Sub
        'Strsql = "Delete from item_vendor where vendorid = " & GridVendor.Rows(lRow).Cells(0).Value & " and itemnumber = " & Me.TxtItemNumber.Text
        'Mysql1.MySQLExecute(Strsql)
        'Load_Data()
        'MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)
    End Sub

    Private Sub BtnDeleteVendor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDeleteVendor.Click
        If MsgBox("คุณต้องการลบข้อมูลใช่หรือไม่?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No Then Exit Sub
        Strsql = "Delete from item_vendor where vendorid = " & GridVendor.Rows(lRow).Cells(0).Value & " and itemnumber = " & Me.TxtItemNumber.Text
        Mysql1.MySQLExecute(Strsql)
        Load_Data()
        MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)
    End Sub
    Private Sub TabGeneral_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabGeneral.GotFocus
        Me.TxtBarcode.Focus()
        TxtBarcode.Select(0, TxtBarcode.Text.ToString.Length)
    End Sub

    Private Sub TabGeneral_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabGeneral.MouseClick
        Me.TxtBarcode.Focus()
        TxtBarcode.Select(0, TxtBarcode.Text.ToString.Length)
    End Sub

    Private Sub TabItem_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabItem.MouseClick
        Select Case TabItem.SelectedIndex
            Case 0
                lRow = 0
                lCol = 0
                Me.TxtBarcode.Focus()
                TxtBarcode.Select(0, TxtBarcode.Text.ToString.Length)
            Case 1
                lRow = 0
                lCol = 0
            Case 2
                lRow = 0
                lCol = 0
            Case 3
                lRow = 0
                lCol = 0
            Case 4
                lRow = 0
                lCol = 0
        End Select


    End Sub

    Private Sub GridUnit_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridUnit.CellClick
        lRow = e.RowIndex
        lCol = e.ColumnIndex
    End Sub

    Private Sub GridVendor_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridVendor.CellClick
        lRow = e.RowIndex
        lCol = e.ColumnIndex
    End Sub

    Private Sub TxtBarcode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtBarcode.TextChanged

    End Sub
End Class
